package com.hh.org.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hh.basic.util.PageList;
import com.hh.org.domain.Dept;
import com.hh.org.mapper.DeptMapper;
import com.hh.org.query.DeptQuery;
import com.hh.org.service.IDeptService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 黄浩
 * @create 2022-06-01 17:55
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DeptServiceImpl extends ServiceImpl<DeptMapper,Dept> implements IDeptService {
    @Resource
    private DeptMapper deptMapper;


    /**
     * 新增
     * @param dept
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void mySave(Dept dept) {
        deptMapper.insert(dept);
        this.myUpdate(dept);
    }

    /**
     * 修改
     * @param dept
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void myUpdate(Dept dept) {
        String dirPath = "";
        if (dept.getParent()==null){
            //前端传来的dept的parent是空，说明是顶级部门
            dirPath = "/"+dept.getId();
        }else {
            //前端传来的dept的parent不是空，说明是儿子部门，需要在前面拼接父部门
            Dept parent = deptMapper.selectById(dept.getParent().getId());
            dirPath = parent.getDirPath()+"/"+dept.getId();
        }
        dept.setDirPath(dirPath);
        deptMapper.updateById(dept);
    }



    /**
     * 高级＋分页查询
     * @param deptQuery
     * @return
     */
    @Override
    public PageList<Dept> queryPage(DeptQuery deptQuery) {
        List<Dept> deptList = deptMapper.queryPage(deptQuery);
        Integer total = deptMapper.getTotal(deptQuery);
        return new PageList<>(total,deptList);
    }

    /**
     * 获得部门树
     * @return
     */
    @Override
    public List<Dept> getDeptTree() {
        //所有部门
        List<Dept> depts = deptMapper.selectList(null);
        //所有部门转为map集合
        Map<Long, Dept> deptMap = depts.stream().collect(Collectors.toMap(Dept::getId, dept -> dept));
        //没有上级的顶级部门 的集合
        List<Dept> topDept = new ArrayList<>();

        for (Dept temp : depts) {
            if (temp.getParentId()==null){
                //上级id为空，加到顶级部门的集合中
                topDept.add(temp);
            }else {
                //上级id不为空,将儿子加到爹的children属性中
                //通过父亲id，找到父亲对象
                Dept parent = deptMap.get(temp.getParentId());
                //将儿子添加到父亲对象的children属性中
                if (parent!=null){
                    parent.getChildren().add(temp);
                }
            }
        }

        return topDept;
    }
}
